<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章管理 - {{ settings.site_title }}</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body class="admin-body">
    <div class="admin-layout">
        <nav class="admin-sidebar">
            <div class="sidebar-header">
                <h3>管理后台</h3>
            </div>
            <ul class="sidebar-menu">
                <li><a href="{{ url_for('admin_dashboard') }}">仪表板</a></li>
                <li><a href="{{ url_for('admin_posts') }}" class="active">文章管理</a></li>
                <li><a href="{{ url_for('admin_pages') }}">页面管理</a></li>
                <li><a href="{{ url_for('admin_settings') }}">网站设置</a></li>
                <li><a href="{{ url_for('admin_logout') }}">退出登录</a></li>
            </ul>
        </nav>

        <main class="admin-main">
            <header class="admin-header">
                <h1>文章管理</h1>
                <a href="{{ url_for('admin_post_editor') }}" class="btn btn-primary">新建文章</a>
            </header>

            <div class="admin-content">
                <div class="content-list">
                    {% if posts %}
                        <table class="content-table">
                            <thead>
                                <tr>
                                    <th>标题</th>
                                    <th>URL别名</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for post in posts %}
                                <tr>
                                    <td>
                                        <a href="{{ url_for('post_detail', slug=post.slug) }}" target="_blank" class="content-title">
                                            {{ post.title }}
                                        </a>
                                    </td>
                                    <td>{{ post.slug }}</td>
                                    <td>
                                        <span class="status-badge {% if post.published %}status-published{% else %}status-draft{% endif %}">
                                            {{ '已发布' if post.published else '草稿' }}
                                        </span>
                                    </td>
                                    <td>{{ post.created_at.split('T')[0] }}</td>
                                    <td>
                                        <div class="action-buttons">
                                            <a href="{{ url_for('admin_post_editor', post_id=post.id) }}" class="btn btn-sm btn-secondary">编辑</a>
                                            <button class="btn btn-sm btn-danger" onclick="deletePost({{ post.id }}, '{{ post.title }}')">删除</button>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    {% else %}
                        <div class="empty-state">
                            <p>还没有任何文章。</p>
                            <a href="{{ url_for('admin_post_editor') }}" class="btn btn-primary">创建第一篇文章</a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </main>
    </div>

    <script>
        function deletePost(postId, postTitle) {
            if (confirm(`确定要删除文章 "${postTitle}" 吗？此操作不可撤销。`)) {
                const formData = new FormData();
                formData.append('content_type', 'post');
                formData.append('id', postId);
                
                fetch('/admin/delete_content', {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert(data.message);
                        location.reload();
                    } else {
                        alert(data.message);
                    }
                })
                .catch(error => {
                    console.error('删除错误:', error);
                    alert('删除失败，请重试');
                });
            }
        }
    </script>
</body>
</html>
